$(document).ready(function(){
    (function(){
        $(".log-in").removeClass("not-select");
        $(".enroll").addClass("not-select");
        $(".register-box").hide();
        $(".login-box").show();
        $(".log-in,.enroll").click(function(){
             toggle()
        });
        //登陆注册切换
        console.log(location.hash)
        if(location.hash == '#register'){

            toggle()
        }
        function toggle(){
            $(".log-in").toggleClass("not-select");
            $(".enroll").toggleClass("not-select");
            $(".login-box").toggle(0);
            $(".register-box").toggle(0);
        }
    })()



    var mobileInput = $('#mobile-number')
    var codeInput = $('#test-code')
    var passInput = $('#set-password')
    var repassInput = $('#reset-password')
    var getCodeButton = $('div.register-box > form > div > a')
    var errorArea = $('div.register-box > form > p > b')
    var defaultMessage = '请填写您的信息'
    var defaultGetCodeMeassage = getCodeButton.text()
    var codeTime = 30;

    errorArea.text(defaultMessage)

    //手机号
    function checkTel(callback){
        var tel=mobileInput.val();
        api.solo('checkTel',[tel],function(d){
            if(d.state==200){
                if(callback) callback();
            }else{
                errorArea.text(d.title)
            }
        })
    }
    mobileInput.change(function(event) {
        checkTel()
    });

    //发送验证码
    getCodeButton.click(function(e){
        e.preventDefault()
        var tel=mobileInput.val();
        var type='register';
        checkTel(function(){
            api.solo('getCode',[tel,type],function(d){
                if(d.state==200){
                    alert('验证码已发送请查收')
                }else{
                    errorArea.text(d.title)
                }
            })
        })
        
    })

    //验证验证码
    codeInput.bind("input propertyChange", function(){
        var tel=mobileInput.val();
        var code = codeInput.val();
        if(code.length!=6){
            $('.code-right').hide();
            $('.code-wrong').show();
            errorArea.text("验证码错误");
            return false;
        }
        api.solo('checkCode',[tel,code],function(d){
            if(d.state==200){
                $('.code-right').show();
                $('.code-wrong').hide();
                errorArea.text("");
            }else{
                $('.code-right').hide();
                $('.code-wrong').show();
                errorArea.text(d.title)
            }
        })
    })

    //提交验证
    console.log($('#register-box form'))
    var regForm  = $('.register-box form')
    regForm.submit(function(event) {
        var tel  = mobileInput.val();
        var code = codeInput.val();
        var pass = passInput.val()
        var repass = repassInput.val()
        if(!tel||!code||!pass||!repass){
            alert('请填写完整')
            event.preventDefault()
        }

        if(pass!=repass){
            errorArea.text('两次输的密码不一样')
            event.preventDefault()
        }else if(pass.length<6){
            errorArea.text('密码不少于六位')
            event.preventDefault()
        }
        return true;
    });
});


